package com.lht.syt.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lht.syt.common.result.Result;
import com.lht.syt.common.result.ResultCodeEnum;
import com.lht.syt.common.utils.MD5;
import com.lht.syt.hosp.service.HospitalSetService;
import com.lht.syt.model.hosp.HospitalSet;
import com.lht.syt.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author lht
 * @version 1.0
 * @description: TODO
 * @date 2024/10/31 17:58
 */

@Api(tags = "医院管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取医院信息列表")
    @GetMapping("findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> hospitalList = hospitalSetService.list();
        return Result.ok(hospitalList);
    }

    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "分页待条件查询")
    @GetMapping("{current}/{size}")
    public Result findPageHospSet(@PathVariable long current, @PathVariable long size,
                                  @ModelAttribute HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page = new Page<>(current, size);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        String hoscode = hospitalSetQueryVo.getHoscode();
        String hosname = hospitalSetQueryVo.getHosname();
        if (!StringUtils.isEmpty(hoscode)) {
            queryWrapper.like("hoscode", hoscode);
        }
        if (!StringUtils.isEmpty(hosname)) {
            queryWrapper.like("hosname", hosname);
        }
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, queryWrapper);
        return Result.ok(hospitalSetPage);
    }

    @ApiOperation(value = "添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        //调用service
        boolean save = false;
        try {
            save = hospitalSetService.save(hospitalSet);
            if (save) {
                return Result.ok();
            } else {
                return Result.fail();
            }
        } catch (DuplicateKeyException e) {
            return Result.build(null, ResultCodeEnum.HOSCODE_EXIST);
        }

    }

    @ApiOperation(value = "单个获取")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        HospitalSet hospitalSetServiceById = hospitalSetService.getById(id);
        return Result.ok(hospitalSetServiceById);
    }

    @ApiOperation(value = "单个修改")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean updateById = hospitalSetService.updateById(hospitalSet);
        if (updateById) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        boolean removeByIds = hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "医院账户设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable @ApiParam(value = "1-锁定，0-解锁") Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    @ApiOperation(value = "发送签名秘钥")
    @PutMapping("sendKey/{id}")
    public Result sendHospitalSetKey(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        int i = 1/0;
        //TODO 发送短信
        return Result.ok();
    }
}
